<?php
declare (strict_types = 1);
/**
 * 管理员
 */
namespace app\common\model;

class Admin extends BaseModel
{

    public function adminRoleHasOne()
    {
        return $this->hasOne(AdminRole::class, 'id', 'roleId');
    }

    /**
     * 管理员列表
     */
    public function adminList($whereAdmin = [], $whereRole = [], $offset = 0, $length = 20)
    {

        $field = ['id', 'name', 'loginIp', 'enable', 'roleId', 'addTime', 'editTime'];

        $fieldRole = ['id', 'name'];

        $list = self::with([
            'adminRoleHasOne' => function ($query) use ($whereRole, $fieldRole) {
                $query->field($fieldRole)->hidden(['id'])->where($whereRole);
            }
        ])->field($field)->where($whereAdmin)->limit($offset, $length)->order(['id' => 'desc'])->select();

        $count = self::where($whereAdmin)->count('id');

        return [
            'list'  => $list,
            'count' => $count,
        ];
    }

}
